<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\RoomsResource;
use App\Models\Episode;
use App\Models\History;
use App\Models\Like;
use App\Models\Task;
use App\Models\TaskLog;
use App\Models\Trace;
use App\Models\Type;
use App\Models\Video;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Models\Room;
use Illuminate\Http\Request;

class TracesController extends Controller
{
    /***
     *我的追剧列表
     * @param Request $request
     * @param Episode $episode
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request,Trace $trace)
    {
        if(empty($this->user)){
            return  $this->error('未登录');
        }
        $ep=$this->user->traces()->orderByDesc('created_at')->paginate(10);
        if($ep->isEmpty()){
            return $this->error('没有追剧记录');
        }
        $ep=$ep->toArray();
        foreach ($ep['data'] as &$item){
            $item['cover']=dealUrl($item['cover']);
            $item['cover_crosswise'] = dealUrl($item['cover_crosswise']);
            $uplist=Video::where('episodes_id',$item['id'])->max('sort');
            $item['uplist']=empty($uplist)?0:$uplist;
            $video_id = History::where('user_id', $this->user->id)->where('traces_id', $item['id'])->max('video_id');
            $looking=Video::where('id',$video_id)->value('sort');
            $item['looking']=empty($looking)?0:$looking;
        }
        return $this->success('success',$ep);
    }


    /***
     * 添加追剧
     * @param Request $request
     * @param Episode $episode
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request,Trace $trace)
    {
        if(empty($this->user)){
            return  $this->error('未登录');
        }
        $ep=Episode::find($request->id);
        if(empty($ep)){
            return  $this->error('该剧不存在');
        }
        $tracess = $trace::where('user_id', $this->user->id)->select('id')->get();
        if (count($tracess) == 1) {
            $task = Task::find(3);
            TaskLog::add($this->user->id, $task->id, $task->price);
        }
        $data=[
            'user_id'=>$this->user->id,
            'episodes_id'=>$request->id,
        ];
        $is_cun=$trace->updateOrCreate($data,$data);
        return $this->success('success',$is_cun);
    }

    /***
     * 停止追剧
     * @param Trace $trace
     * @return \Illuminate\Http\JsonResponse
     */
    public function rezuiju(Request $request, Trace $trace)
    {
        if ($request->has('id') && !is_numeric($request->id)) {
            return $this->error('id err');
        }
//            $ret=$trace->where('user_id',$this->user->id)->delete();//软删除
        $ret = $trace->where('user_id', $this->user->id)->where('episodes_id', $request->id)->forceDelete();//永久删除
//        $ret=$trace->where('user_id',$this->user->id)->restore();//恢复软删除
            if($ret){
                return $this->success('删除成功');
            }else{
                return $this->error('删除失败');
            }
    }

    /***
     * 最近播放与观看历史都调用这个接口
     * @param Request $request
     * @param History $history
     * @return \Illuminate\Http\JsonResponse
     */
    public function recentPlay(Request $request,History $history)
    {
        if(empty($this->user)){
            return  $this->error('未登录');
        }
        $list = $this->user->history()->orderByDesc('historys.updated_at')->paginate(100)->toArray();//->groupByRaw('traces_id')
//        dd($list);
        foreach ($list['data'] as &$item){
            $item['cover']=dealUrl($item['cover']);
            $item['cover_crosswise'] = dealUrl($item['cover_crosswise']);
            $uplist=Video::where('episodes_id',$item['id'])->max('sort');
            $item['uplist'] = empty($uplist) ? 0 : $uplist;//更新到多少集了
            $video_id = History::where('user_id', $this->user->id)->where('traces_id', $item['id'])->max('video_id');
            $looking=Video::where('id',$video_id)->value('sort');
            $item['looking'] = empty($looking) ? 0 : $looking;//看到多少集了
            $item['is_zui']=Trace::where('user_id',$this->user->id)->where('episodes_id',$item['id'])->exists()?1:0;
        }
        if(empty($list)){
            return $this->error('没有播放记录');
        }
        return $this->success('success',$list);
    }

    /***
     *排行榜
     * @return \Illuminate\Http\JsonResponse
     */
    public function recommend()
    {
        if (empty($this->user)) {
            return $this->error('未登录');
        }
        $episode = History::with('episode')
            ->select('id','user_id','video_id','traces_id', DB::raw('count(traces_id) as traces_count'))
            ->groupBy('traces_id')
            ->orderByDesc('traces_count')
            ->paginate(10)->toArray();
        foreach ($episode['data'] as &$item){
            $item['episode']['cover']=dealUrl($item['episode']['cover']);
            $item['episode']['cover_crosswise'] = dealUrl($item['episode']['cover_crosswise']);
            $uplist=Video::where('episodes_id',$item['episode']['id'])->max('sort');
            $item['episode']['uplist']=empty($uplist)?0:$uplist;
            $video_id = History::where('user_id', $this->user->id)->where('traces_id', $item['episode']['id'])->max('video_id');
            $looking = Video::where('id', $video_id)->value('sort');
            $item['episode']['looking'] = empty($looking) ? 0 : $looking;
            if($this->user){
                $item['episode']['is_zui']=Trace::where('user_id',$this->user->id)->where('episodes_id',$item['episode']['id'])->exists()?1:0;
            }else{
                $item['episode']['is_zui']=0;
            }
        }
        return $this->success('success',$episode);
    }

    /***
     * 推荐
     * @param Episode $episode
     * @return \Illuminate\Http\JsonResponse
     */
    public function tuijian(Episode $episode)
    {
        $query=$episode->newQuery();
        $list=$query->withCount('History')
            ->with('History')
            ->orderByRaw('is_tui desc,history_count desc')->paginate(10)->makeHidden(['status', 'History', 'deleted_at', 'unionid', 'type_id', 'created_at', 'updated_at'])->toArray();
//        dd($list);
        foreach ($list as &$item){
            $item['cover']=dealUrl($item['cover']);
            if($this->user){
                $item['is_zui']=Trace::where('user_id',$this->user->id)->where('episodes_id',$item['id'])->exists()?1:0;
            }else{
                $item['is_zui']=0;
            }
        }
        return $this->success('success',$list);
    }
}
